/**
 *
 */
package cn.hg.jfsa.manage.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

import cn.hg.base.jfinal.controller.BaseController;
import cn.hg.jfsa.core.JfsaThreadLocal;
import cn.hg.jfsa.model.JfsaAdmin;

/**
 * @author golly
 */
public class LoginInterceptor implements Interceptor {

	/*
	 * (non-Javadoc)
	 * @see com.jfinal.aop.Interceptor#intercept(com.jfinal.aop.Invocation)
	 */
	@Override
	public void intercept (Invocation inv) {
		JfsaAdmin admin = JfsaThreadLocal.getAdmin();

		if (admin == null) { // 如果Session中不存在用户登录信息

			if (inv.getController() instanceof BaseController) {
				BaseController c = (BaseController) inv.getController();

				if (c.isAjaxRequest()) { // 如果是Ajax请求
					c.renderAjaxFailed("-1", "登录超时，请重新登录！");
					return;
				} else {

					if ("/manage".equals(inv.getActionKey()) || "/manage/index".equals(inv.getActionKey())
						|| "/manage/logout".equals(inv.getActionKey())) {
						c.redirect("/manage/login");
					} else {
						c.render("/manage/timeout.html");
					}

					return;
				}

			}

		} else {
			inv.getController().setAttr("ADMIN", admin);
			inv.invoke();
		}

	}

}
